/*
* $Id: AddPointView.cpp 108 2010-06-29 16:54:39Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddPointView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Settings/Journal/PointViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"
#include "Database/Operation/AddPointOperation.h"

#define TITLE_ID		1
#define NAME_POINT_ID	2
#define NOTE_POINT_ID	3
#define USER_FIELD_ID	4

CAddPointView::CAddPointView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CAddPointView::~CAddPointView(){}

void CAddPointView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(IApplication::GetApp()->GetLanguageManager()->GetString(ADD_POINT_ID), TITLE_ID));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_POINT_NAME_ID), NAME_POINT_ID, editItem::TEXT));
	CPointViewSettings pSettings;
	if(pSettings.GetState(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_POINT_ID, editItem::TEXT));
	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(CViewFieldValuesHelper::POINTS, USER_FIELD_ID, 0);
}

void CAddPointView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_ADD_POINT;
	m_pState->m_nIdParameter = 0;
}
bool CAddPointView::IsViewState(State state){
	return state.m_type == JournalEnum::ADD_POINT;
}

BEGIN_MESSAGE_MAP(CAddPointView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CAddPointView::OnSavePoint(){
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CAddPointOperation operation;
	operation.m_content.s_strName = m_editorDrawer.GetValueItem(NAME_POINT_ID);
	operation.m_content.s_strNote = m_editorDrawer.GetValueItem(NOTE_POINT_ID);
	for(int i = 1; i <= m_nUserFields; i++)
		operation.m_content.s_aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	operation.AddValue(pDb);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	__super::OnCancel();
}